package com.luo.chengrui.module.sa.service.auth;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServerException;
import com.luo.chengrui.module.sa.dal.redis.RedisKeyConstants;
import com.luo.chengrui.module.sa.service.auth.AuthorizeExcludeService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.luo.chengrui.module.sa.controller.admin.auth.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.auth.AuthorizeExcludeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import com.luo.chengrui.module.sa.convert.auth.AuthorizeExcludeConvert;
import com.luo.chengrui.module.sa.dal.mysql.auth.AuthorizeExcludeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.luo.chengrui.module.sa.enums.ErrorCodeConstants.*;;

/**
 * 被排除的角色  Service 实现类
 *
 * @author 诚睿
 */
@Service
@Validated
public class AuthorizeExcludeServiceImpl implements AuthorizeExcludeService {

    @Resource
    private AuthorizeExcludeMapper authorizeExcludeMapper;

    @Override
    @CacheEvict(value = RedisKeyConstants.SA_PSM_AUTH_LIST, key = "#createReqVO.orgFullId")
    public String createAuthorizeExclude(AuthorizeExcludeCreateReqVO createReqVO) {
        // 插入
        AuthorizeExcludeDO authorizeExclude = AuthorizeExcludeConvert.INSTANCE.convert(createReqVO);
        authorizeExcludeMapper.insert(authorizeExclude);
        // 返回
        return authorizeExclude.getId();
    }

    @Override
    @CacheEvict(value = RedisKeyConstants.SA_PSM_AUTH_LIST, key = "#createReqVO.orgFullId")
    public int cancelExcludedAuthorize(AuthorizeExcludeCreateReqVO createReqVO) {
        if (StrUtil.isBlank(createReqVO.getRoleId()) || StrUtil.isBlank(createReqVO.getOrgFullId())) {
            throw new ServerException(AUTHORIZE_ORG_OR_ROLE_IS_NOT_NULL);
        }
        AuthorizeExcludeDO authorizeExclude = AuthorizeExcludeConvert.INSTANCE.convert(createReqVO);
        // 直接进行物理删除
        return authorizeExcludeMapper.deleteByEntity(authorizeExclude);
    }
}
